home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / ascutils.zip / RMCOL.ASM < prev    next >
Assembly Source File  |  1988-05-20  |  5KB  |  192 lines

  1. PAGE 55,132
  2. ;                RMCOL
  3. ;    This program takes lines from std input    and trims 'count' columns
  4. ;    beginning with column 'start+1'.  Syntax is:
  5. ;    RMCOL n1<delim>n2  [<file1] [>[>file2]]    where n1 is the    start col-1
  6. ;    (0 to 256), n2 is the #    of cols    to remove (may be 0), <delim> is any
  7. ;    number of tabs or spaces with an optional single comma,    and files
  8. ;    are as usual for std in    and std    out.
  9.  
  10. codeseg        segment
  11.         assume    cs:codeseg, ds:codeseg
  12.  
  13.         org    100h
  14.  
  15. rmcol        proc    far
  16.  
  17. start:        jmp    Main
  18.  
  19. errmsg        db    0Dh, 0Ah, 'RMCOL: Invalid arguments', 0Dh, 0Ah,    '$'
  20. start_col    dw    0
  21. col_count    dw    0
  22.  
  23.  
  24. Main:        mov    si,81h            ; point    si to PSP
  25.         call    skip_spc        ; skip white space
  26.         call    get_dec            ; get first number
  27.         jc    error            ; cf=1 on error
  28.         mov    start_col,cx        ; save value
  29.         call    skip_spc
  30.         call    get_dec
  31.         jc    error
  32.         mov    col_count,cx        ; save column count
  33. Main_loop:    call    get_line        ; get a    line
  34.         jc    Exit            ; cf=1 on error    or eof
  35.         call    doit            ; remove the columns
  36.         call    put_line        ; output the line
  37.         jmp    short Main_loop        ; continue until cf=1
  38.  
  39. error:        mov    dx,offset errmsg    ; error    message    exit
  40.         mov    ah,9
  41.         int    021h
  42. Exit:        int    020h            ; program exit.
  43.  
  44. rmcol        endp
  45.  
  46.  
  47. ;        SUBROUTINE
  48. ;        skips white space
  49.  
  50. skip_spc    proc    near
  51. skip1:        cmp    byte ptr [si],020h
  52.         jne    skip2
  53.         inc    si
  54.         jmp    short skip1
  55. skip2:        cmp    byte ptr [si],9
  56.         jne    skip_ret
  57.         inc    si
  58.         jmp    short skip1
  59. skip_ret:    ret
  60. skip_spc    endp
  61.  
  62.  
  63. ;        SUBROUTINE
  64. ;        converts ASCII decimal int to binary int
  65.  
  66. get_dec        proc    near
  67.         xor    cx,cx        ; start    with cx=0
  68.  
  69. dec1:        mov    al,[si]        ; get char
  70.         cmp    al,020h        ; space    indicates end of numerals
  71.         je    dec_ret
  72.         cmp    al,9        ; so does tab
  73.         je    dec_ret
  74.         inc    si
  75.         cmp    al,02Ch        ; and comma
  76.         je    dec_ret
  77.         dec    si
  78.         cmp    al,03Ah        ; ascii    range of char ok so far
  79.         jb    dec2
  80.         stc            ; ascii    out of range - exit cf=1
  81.         jmp    short dec_ret
  82.  
  83. dec2:        sub    al,030h        ; if numeral, cvt to binary
  84.         jc    dec_ret        ; not a    numeral, exit cf=1
  85.         shl    cx,1        ; cx = cx * 2
  86.         mov    dx,cx        ; save cx * 2
  87.         shl    cx,1        ; cx * 4
  88.         shl    cx,1        ; cx * 8
  89.         add    cx,dx        ; cx * 8  +  cx    * 2 = cx * 10d
  90.         cbw            ; convert al byte to word
  91.         add    cx,ax        ; add 1's to 10's
  92.         mov    ax,0FFh        ; 255d = max line length
  93.         cmp    ax,cx        ; if cx    > 255 then error
  94.         jb    dec_ret
  95.         inc    si        ; otherwise continue.
  96.         jmp    short dec1
  97.  
  98. dec_ret:    ret        ; ret with cf=1    for error or cf=0 for success
  99. get_dec        endp
  100.  
  101.  
  102. ;        SUBROUTINE
  103. ;        gets line from std input
  104.  
  105. get_line    proc    near
  106.         mov    si,offset buffer    ; point    to input line buffer
  107.  
  108. get_1:        mov    dx,si            ; chars    are input to ds:dx
  109.         mov    cx,1            ; char count
  110.         mov    bx,0            ; std input handle
  111.         mov    ah,3Fh            ; get char from    std in
  112.         int    21h
  113.         jc    get_line_ret        ; error    - exit cf=1
  114.         cmp    byte ptr [si],0Ah
  115.         je    get_line_ret
  116.         mov    cx,ax            ; get #    input chars into cx
  117.         cmp    byte ptr [si],1Ah    ; check    for eof
  118.         stc
  119.         jz    get_line_ret        ; eof -    exit cf=1
  120.         jcxz    get_line_ret        ; 0 chars input    - exit cf=1
  121.         inc    si            ; point    to next    char
  122.         cmp    si,offset buffer+254    ; max line - 2
  123.         jae    get_end            ; truncate long    lines.
  124.         jmp    short get_1        ; else continue.
  125.  
  126. get_end:    mov    byte ptr [si],0Dh
  127.         mov    byte ptr [si+1],0Ah
  128.         clc
  129.  
  130. get_line_ret:    ret
  131. get_line    endp
  132.  
  133.  
  134. ;        SUBROUTINE
  135. ;        This is    where the columns get removed
  136.  
  137. doit        proc    near
  138.         mov    cx,start_col
  139.         mov    si,offset buffer    ; input    buffer
  140.         mov    di,offset buffer+256    ; copy buffer
  141.  
  142. start_loop:    jcxz    doit_1        ; start    removing at col    1
  143.         lodsb            ; otherwise, copy start_col chars
  144.         stosb            ; to copy buffer
  145.         cmp    al,0Dh        ; (premature eoln)
  146.         je    doit_2
  147.         loop    start_loop    ; loop cx to 0
  148.  
  149. doit_1:        mov    cx,col_count    ; get count in cx
  150.  
  151. middle_loop:    jcxz    doit_2        ; col count 0 ?    copy whole line.
  152.         lodsb            ; else skip over cx chars.
  153.         cmp    al,0Dh        ; premature eoln, finish up now.
  154.         je    end_loop
  155.         loop    middle_loop
  156.  
  157.         jmp    short doit_2    ; if middle loop was executed, skip
  158.                     ; this next stosb instr.
  159. end_loop:    stosb
  160. doit_2:        lodsb
  161.         cmp    byte ptr [si-2],0Ah    ; now copy chars until eoln.
  162.         jne    end_loop
  163.         ret
  164. doit        endp
  165.  
  166.  
  167. ;        SUBROUTINE
  168. ;        Output line to std output
  169.  
  170. put_line    proc    near
  171.         mov    si,offset buffer+256    ; set up pointers to the
  172.         mov    dx,si            ; copy (output)    buffer.
  173.         xor    cx,cx
  174.  
  175. loc_6:        inc    cx
  176.         lodsb
  177.         cmp    al,0Ah
  178.         jne    loc_6        ; count    chars to eoln into cx
  179.         mov    bx,1        ; handle for std out
  180.         mov    ah,40h
  181.         int    21h
  182.         ret            ; cf=1 if error
  183. put_line    endp
  184.  
  185. buffer        db    0        ; line input buffer - this makes the
  186.                 ; program look smaller,    but it uses 512    bytes
  187.                 ; past the 'end' of the    coded listing.    This
  188.                 ; may be extended arbitrarily, but with    risk.
  189.  
  190. codeseg        ends
  191.         end    start
  192.